package edu.stanford.prpl.android;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.content.Intent;
import android.content.res.Resources;
import java.io.*;
import java.net.*;
import java.util.HashMap;

import edu.stanford.prpl.facade.*;

public class FacadeAndroid extends Activity 
{
	private static final String TAG = "FacadeAndroid";
	// public static UIEngine android_facade_ui_engine = null;
	
	private Object UIresult = null;


	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        Log.d(TAG, "Facade Android starting... \n");
        String outputString = "Facade for Android";
        
        try
        {
        	// Initialize UI Engine with UI template specs
        	AppActivity.androidUI_engine = new UIEngine(UIEngine.devicePlatform.ANDROID);
        	// String templateFilePath = "DynNaviList.template.xml";
        	InputStream templateStream = getResources().openRawResource(R.raw.prpl_facade_dynnavilist_template);        
        	AppActivity.androidUI_engine.setTemplate(new BufferedReader(new InputStreamReader(templateStream)));        

        	// Initial XML data URL
        	// String jinzoraMainPageXML_url = "http://prpl.stanford.edu/music/api.php?user=skteh&password=serangga&request=home";        
        	// = "http://prpl.stanford.edu/music/api.php?user=skteh&pass=serangga&request=browse&jz_path=Classical%2FBach%2C+Johann+Sebastian+(1685+-+1750)";
        	
        	//URL jinzoraData_xml = new URL(jinzoraMainPageXML_url);
        	//BufferedReader in = new BufferedReader(
            //                    	new InputStreamReader(jinzoraData_xml.openStream())
            //                	);
        	// UIresult = android_facade_ui_engine.render(in, "Facade", UIEngine.renderType.NORMAL, this);
        	// UIresult = androidUI_engine.render(new BufferedReader(new FileReader("jinzora_data.xml")), "Jinzora", renderType.NORMAL, this);
        	// UIresult = androidUI_engine.render(new BufferedReader(new FileReader("jinzora_data.xml")), "Jinzora", renderType.AJAX_WEB_FRAGMENT, null);

        }
        catch (Exception e)
        {
        	outputString = "\n\nException: "+e.toString()+" \n";
        	
        	// UI
        	setContentView(R.layout.main);
        	TextView mIntroMessage = (TextView)findViewById(R.id.intro_message);
        	mIntroMessage.setText(outputString);
        	// Log.d(TAG, "\n"+outputString+"\n");
        	return;
        }


        // Initial XML data URL
    	String jinzoraMainPageXML_url = "http://prpl.stanford.edu/music/api.php?user=skteh&password=serangga&request=home";
    	// = "http://prpl.stanford.edu/music/api.php?user=skteh&pass=serangga&request=browse&jz_path=%2F";
    	// = "http://prpl.stanford.edu/music/api.php?user=skteh&pass=serangga&request=browse&jz_path=Classical";
    	
        
    	// Run Facade
    	String pageTitle = "Facade";
    	String actionCall = jinzoraMainPageXML_url;
		
		Intent chosen = new Intent();
		chosen.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		chosen.setData(Uri.parse(actionCall));
		chosen.putExtra("pageTitle", pageTitle);
		chosen.putExtra("actionCall", actionCall);

		chosen.setAction(Intent.ACTION_VIEW);
		chosen.setClass(this, AppActivity.class);
		startActivity(chosen);
		finish();
        	
    }
    
    
}